﻿
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using SageKing.Core.Extensions;
using SageKing.Core.Options; 

namespace SageKing.OpenIddictResource.WebClientApi.Features;
 
public class OpenIdResourceWebClientApiFeature : FeatureBase
{
    public OpenIdResourceWebClientApiFeature(IModule module) : base(module)
    {
    }

    public ResourceWebClientApiOptions Options { get; set; }

    /// <inheritdoc />
    public override void Apply(IConfigurationManager configurationManager, IHostEnvironment hostEnvironment)
    {
        ArgumentNullException.ThrowIfNull(configurationManager);
        if (Options == null)
        {
            Options = new ResourceWebClientApiOptions();
            configurationManager.Bind(Options, Options.SectionName);
        }

        Services.AddSingleton(Options);

        Services.AddHttpClient(HttpClientConsts.HttpClientResourceEndPointUrlKey, client =>
        {
            client.BaseAddress = new Uri(Options.EndPointUrl);
        });

        Services.AddAutoRegisteredServices(typeof(OpenIdResourceWebClientApiFeature).Assembly);
    }
}
